package edu.pku.sei.transformation.editor.views.figures;

import java.util.ArrayList;

import org.eclipse.draw2d.Ellipse;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;

import edu.pku.sei.transformation.editor.model.goal.Description;
import edu.pku.sei.transformation.editor.views.icons.IconsProvider;

public class TransformationGoalFigure extends Ellipse {
	private Label head = new Label("head");
	private Label tag = new Label("<<Goal>>");
	private static final int MARGIN = 0;
	private TextArea toolTip = new TextArea();
	private boolean isOptional = false;

	public TransformationGoalFigure(boolean isOptional) {
		super();
		if (isOptional) {
			this.isOptional = true;
			setLineStyle(SWT.LINE_DASH);
		}
		this.setLayoutManager(new XYLayout());

		tag.setFont(FontConstant.STEREO_TYPE_LABEL);
		this.add(tag);

		head.setFont(FontConstant.ARIAL_9_B);
		head.setIcon(IconsProvider.getImage(IconsProvider.GOAL_ICO));
		this.add(head);
	}

	@Override
	public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		int middle = rect.y + rect.height / 2;

		head.setLocation(new Point(rect.x, middle
				- head.getPreferredSize().height / 2));
		head.setSize(rect.width, head.getPreferredSize().height);

		tag.setLocation(new Point(rect.x, head.getBounds().y - MARGIN
				- tag.getPreferredSize().height));
		tag.setSize(rect.width, tag.getPreferredSize().height);

	}

	public void setName(String s) {
		head.setText(s);
	}

	public void refreshToolTipFigure(Description goalDes,
			ArrayList<Description> descriptions) {
		toolTip = new TextArea() {
			@Override
			public Dimension getPreferredSize(int width, int h) {
				return super.getPreferredSize(200, h);
			}
		};

		StringBuffer sb = new StringBuffer();
		// if (!isOptional)
		// sb.append("Goal:\n");
		// else
		// sb.append("Optional Goal:\n");
		//		
		// sb.append(head.getText()+"\n\n");
		sb.append("Description:\n");
		sb.append(goalDes.getContent() + "\n");

		if (descriptions != null) {
			for (Description des : descriptions) {
				sb.append("\n");
				sb.append(des.getSummary() + ":\n");
				sb.append(des.getContent().trim() + "\n");

			}
		}

		toolTip.setText(sb.toString());

		this.setToolTip(toolTip);
	}

}
